<?php

/**
 * plates模板引擎，使用原生php
 * @return League\Plates\Engine|mixed|null
 */
function view_plates()
{
    $templates = null;
//    dump_pre(getGlobals('view_plates'));
    if (!getGlobals('view_plates')) {
        $templates = new League\Plates\Engine(VDir);
        setGlobals('view_plates', $templates);
    }
//    dump_pre($templates);
    return getGlobals('view_plates');
}

/**
 * twig视图工具类实例
 * @return Twig\Environment
 */
function view_twig()
{
    $twig = null;
    if (!getGlobals('view_twig')) {
        $loader = new Twig\Loader\FilesystemLoader(VDir);
        $twig = new Twig\Environment($loader, array(
            'cache' => CacheDir . 'twig_cache',
            'debug' => DEBUG, 'auto_reload' => DEBUG,
        ));
        if (DEBUG) {
            $twig->addExtension(new Twig\Extension\DebugExtension());
        }

        $twig->addFunction(new \Twig\TwigFunction('getCSS', 'getCSS'));
        $twig->addFunction(new \Twig\TwigFunction('getGet', 'getGet'));
        $twig->addFunction(new \Twig\TwigFunction('explode', 'explode'));
        //演示给twig增加过滤器函数的方法
        //增加markdown过滤器
        $twig->addFilter(new Twig\TwigFilter("markdown", function ($str) {
            return markdown($str);
        }));

        if (MLang) {
            //增加翻译功能
            $twig->addFilter(new Twig\TwigFilter("t", function ($str, ...$args) {
                //var_dump($args);
                return _t($str, ...$args);
            }));
        }

        setGlobals('view_twig', $twig);
    }

    return getGlobals('view_twig');
}

use eftec\bladeone\BladeOne;

class blade extends eftec\bladeone\BladeOne
{
    public function getTemplateFile($templateName = ''): string
    {
        if (substr($templateName, strlen($this->fileExtension) * -1) != $this->fileExtension) {
            $templateName .= $this->fileExtension;
        }
        return parent::getTemplateFile($templateName);
    }
}

/**
 * smarty视图工具类实例
 * @return BladeOne
 */
function view_blade()
{
    $blade = null;
    if (!getGlobals("view_blade")) {
        if (!file_exists(CacheDir . 'blade')) {
            mkdir(CacheDir . 'blade');
        }
        if (DEBUG) {
            $blade_debug = eftec\bladeone\BladeOne::MODE_DEBUG;
        } else {
            $blade_debug = eftec\bladeone\BladeOne::MODE_AUTO;
        }
        $blade = new blade(VDir, CacheDir . 'blade', $blade_debug);
        $blade->setFileExtension('.blade.php');
        setGlobals('view_blade', $blade);
    }

    return getGlobals("view_blade");
}

/**
 * mustache视图工具类实例
 * @return Mustache_Engine
 */
function view_mustache()
{
    $mustache = null;
    if (!getGlobals("view_mustache")) {
        $mustache = new Mustache_Engine(array(
            'logger' => new Mustache_Logger_StreamLogger('php://stderr'),
            'template_class_prefix' => '_mustache_',
            'cache' => CacheDir . 'mustache_cache',
            'cache_file_mode' => 0666,
            'loader' => new Mustache_Loader_FilesystemLoader(VDir),
            'partials_loader' => new Mustache_Loader_FilesystemLoader(VDir),
            'strict_callables' => true,
            'pragmas' => [Mustache_Engine::PRAGMA_FILTERS, Mustache_Engine::PRAGMA_BLOCKS, Mustache_Engine::PRAGMA_ANCHORED_DOT],
            'delimiters' => MustacheTemplateStart . ' ' . MustacheTemplateEnd
        ));
        $mustache->addHelper('getCss', function ($text) {
            return getCss($text);
        });
        $mustache->addHelper('getGet', function ($text) {
            return getGet($text);
        });

        setGlobals('view_mustache', $mustache);
    }

    return getGlobals("view_mustache");
}

/**
 * 模板引擎，默认配置的模板引擎
 * @return Mustache_Engine|Twig\Environment|BladeOne|league\plates\Engine
 */
function view()
{
    $eng = strtolower(MainTemplateEngine);
    if ($eng == "mustache") {
        return view_mustache();
    } elseif ($eng == "blade") {
        return view_blade();
    } elseif ($eng == "twig") {
        return view_twig();
    } elseif ($eng == "plates") {
        return view_plates();
    } else {
        return view_mustache();
    }
}